from django.db import models
from django.contrib import admin
from django.utils.timezone import now
from user.models import User

# 模型列表【方便复制】
# ProImg,Module,ProType,Project

#模块图片
class ProImg(models.Model):
    """
    模块图片
    """
    del_choices = (('0', '未删除'),('1', '已删除'))
    pro_img =models.ImageField(upload_to='img/pro', verbose_name='模块图片', help_text='模块图片（ImageField）', blank=True)
    created_by = models.ForeignKey(User,verbose_name='创建人ID',help_text='创建人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='pro_img_createdby')
    created_date = models.DateTimeField(verbose_name='创建时间', help_text='创建时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User,verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='pro_img_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除', help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')
    class Meta:
        ordering = ['-id']
        verbose_name = '模块图片'
        verbose_name_plural = verbose_name
    def __str__(self):
        return str(self.pro_img)
@admin.register(ProImg)
class ProImgAdminModel(admin.ModelAdmin):
    list_display = ('id', 'pro_img', 'created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')

#分类菜单数据表模型
class Module(models.Model):
    """
    分类菜单数据表模型
    """
    del_choices = (('0', '未删除'), ('1', '已删除'))
    root = models.PositiveIntegerField(verbose_name='根', default=0)
    name = models.CharField(max_length=60, verbose_name='名称')
    parent = models.ForeignKey('self', verbose_name='父级', null=True, blank=True, on_delete = models.PROTECT, related_name='sub_menu')
    url = models.CharField(max_length=255, blank=True, verbose_name='链接')
    path = models.CharField(max_length=255, verbose_name='路径')
    grade = models.PositiveIntegerField(verbose_name='层级', default=0)
    order = models.PositiveIntegerField(verbose_name='排序', default=0)
    category = models.CharField(max_length=30, verbose_name='类别')
    system = models.CharField(max_length=30, verbose_name='所属系统')
    projects = models.CharField(max_length=120, verbose_name='所属项目', blank=True)
    short = models.CharField(max_length=30, verbose_name='简称')
    desc = models.TextField(verbose_name='描述', blank=True)
    created_by = models.ForeignKey(User,verbose_name='创建人ID',help_text='创建人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='module_createdby')
    created_date = models.DateTimeField(verbose_name='创建时间', help_text='创建时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User,verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='module_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除', choices=del_choices, default='0')
    class Meta:
        ordering = ['-id']
        # 修改admin后台显示名字
        verbose_name = '菜单'
        verbose_name_plural = verbose_name

    def __str__(self):
        # 返回分类名
        return self.name
@admin.register(Module)
class ModuleAdminModel(admin.ModelAdmin):
    list_display = ('name', 'id', 'root', 'parent', 'url', 'path', 'grade', 'order', 'category', 'system','projects', 'short', 'desc','created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')

#项目模块类型
class ProType(models.Model):
    """项目模块类型"""
    del_choices = (('0', '未删除'), ('1', '已删除'))
    name = models.CharField(max_length=90, verbose_name='类型名称', help_text='类型名称（CharField）')
    short_name = models.CharField(max_length=90, verbose_name='类型简写', help_text='类型简写（CharField）')
    created_by = models.ForeignKey(User, verbose_name='创建人ID',help_text='创建人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='protype_createdby')
    created_date = models.DateTimeField(verbose_name='创建时间', help_text='创建时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User, verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='protype_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除',help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')
    class Meta:
        ordering = ['-id']
        verbose_name = '项目类型'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name
@admin.register(ProType)
class ProTypeAdminModel(admin.ModelAdmin):
    """
    注册项目模块类型
    """
    list_per_page = 20
    list_display = ('name', 'short_name','created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')

#项目模块
class Project(models.Model):
    """
    项目模块
    """
    del_choices = (('0', '未删除'), ('1', '已删除'))

    name = models.CharField(max_length=90, verbose_name='项目名称', help_text='项目名称（CharField）')
    pro_type = models.ForeignKey(ProType , verbose_name='项目类型',help_text='项目类型（ForeignKey）', null=True, on_delete=models.SET_NULL)
    pro_logo = models.ForeignKey(ProImg, db_column='pro_logo_id', verbose_name='模块logo',help_text='模块logo（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL,related_name="pro_logo")
    pro_imgs = models.ForeignKey(ProImg, db_column='pro_imgs_id', verbose_name='模块图片',help_text='模块图片（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL,related_name="pros_img")
    # pro_type = models.CharField(max_length=20, verbose_name='项目类型', help_text='项目类型（CharField）', blank=True,null=True)
    desc = models.TextField(verbose_name='项目描述',help_text='项目描述（TextField）', blank=True)
    order = models.PositiveIntegerField(verbose_name='排序', help_text='排序（PositiveIntegerField）', default=0)
    created_by = models.ForeignKey(User, verbose_name='创建人ID',help_text='创建人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='project_createdby')
    created_date = models.DateTimeField(verbose_name='创建时间', help_text='创建时间（DateTimeField）', null=True, blank=True, auto_now_add=True)
    last_edited_by = models.ForeignKey(User, verbose_name='最后编辑人ID',help_text='最后编辑人ID（ForeignKey）', null=True, blank=True, on_delete=models.SET_NULL, related_name='project_lasteditedby')
    last_edited_date = models.DateTimeField(verbose_name='最后编辑时间', help_text='最后编辑时间（DateTimeField）', null=True, blank=True, auto_now=True)
    deleted = models.CharField(max_length=1, verbose_name='是否删除',help_text='是否删除（CharField，可选值：0，1）', choices=del_choices, default='0')
    class Meta:
        ordering = ['order']
        verbose_name = '项目'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
@admin.register(Project)
class ProjectAdminModel(admin.ModelAdmin):
    """
    注册项目模型
    """
    list_per_page = 20
    list_display = ('name', 'pro_type', 'pro_logo','pro_imgs','desc', 'created_by', 'created_date', 'last_edited_by', 'last_edited_date', 'deleted')
